package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
/**
 *
 * @author axi
 *
 */
public class HangzhouNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://auto.hangzhou.com.cn/html/[0-9]+/n-[0-9]+.html",
				"http://.*.hangzhou.com.cn/.*content/[0-9]{4}-[0-9]{1,2}/[0-9]{1,2}/content_[0-9]+.*[.]htm",
				"http://.*.hangzhou.com.cn/content/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url=urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
		}
		String htmltxt=urlMeta.getHtml();
		String title="";
		String content="";
		Long date=null;
		Integer commentNum=0;
		Integer clickNum=0;
		NewsMeta news=new NewsMeta();
		Document doc=Jsoup.parse(htmltxt);
		if(url.indexOf("news.hangzhou.com.cn") != -1){
			title=doc.select("td.xwzx_wname01").text();
			date=DateUtils.matchDate(doc.select("td[height=25]").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.xwzx_wname02"));
		}else if(url.indexOf("auto.hangzhou.com.cn") != -1){
			title=doc.select("td.bcolor02").text();
			date=DateUtils.matchDate(doc.select("td[height=25]").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.wz14"));
		}else if(url.indexOf("http://house.hangzhou.com.cn/") != -1){
			title=doc.select("td.tittle_18px").text();
			date=DateUtils.matchDate(doc.select("td[height=30]").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.lm_14px"));
		}else if(url.indexOf("money.hangzhou.com.cn") != -1){
			title=doc.select("td.le_16black").text();
			date=DateUtils.matchDate(doc.select("td[height=20]").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("table.le_14black"));
		}else if(url.indexOf("ori.hangzhou.com.cn") != -1){
			title=doc.select("td.fontsz16").text();
			date=DateUtils.matchDate(doc.select("td[height=30]").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.fontsz14"));
		}else if(url.indexOf("edu.hangzhou.com.cn") != -1){
			title=doc.select("td.bcolor02").text();
			date=DateUtils.matchDate(doc.select("td[height=25]").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.wz14"));
		}else{
			title=doc.select("td.fontsz20").text();
			date=DateUtils.matchDate(doc.select("td[height=30]").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.fontsz14"));
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("td.hwtk_wname01").text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("td.le_st16black").text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			if(title.contains("-")) title=StringUtils.substringBefore(title, "-");
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select("td[height=25]").text());
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.hwtk_wname02"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.le_st14black"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.Dlife_bcolor02"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.hzwRP_lname06"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.xz_bcolor01"));
		}
		String author=JSoupUtils.matchAuthor(doc, "来源:");
		news.setAuthor(author);
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		news.setUrl(url);
		news.setType(0);
		news.setCommentNum(commentNum);
		news.setClickNum(clickNum);

		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HangzhouNewsAnalyse h=new HangzhouNewsAnalyse();
//		String url="http://news.hangzhou.com.cn/xwzxhz/content/2012-11/21/content_4485171.htm";
//		String url="http://auto.hangzhou.com.cn/html/31/n-39631.html";
//		String url="http://house.hangzhou.com.cn/lsxw/qylp/content/2012-11/19/content_4482121.htm";
//		String url="http://money.hangzhou.com.cn/aksh/content/2012-11/20/content_4483563.htm";
//		String url="http://ori.hangzhou.com.cn/ornews/content/2012-11/21/content_4486487.htm";
		String url="http://hznews.hangzhou.com.cn/xinzheng/yaolan/content/2014-08/11/content_5396677.htm";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=h.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
